/**
 * 
 */
package com.seguriapp.controllers;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.seguriapp.dao.TramitesAttributeDao;
import com.seguriapp.dao.TramitesDao;
import com.seguriapp.entities.TramiteAttributeEntity;
import com.seguriapp.sync.TramitesTask;

/**
 * @author installer
 * 
 */
public class TramitesController {

	private Context mContext;

	public enum PaperworkAttributeCode {
		FileNeeded, Objective, Observation, Place;
	}

	public TramitesController(final Context context) {
		mContext = context;
	}

	public void insertInitialTramites() {
		final TramitesTask tramitesTask = new TramitesTask(mContext);
		tramitesTask.execute();
	}

	public int deleteTramites() {
		mContext.getContentResolver().delete(TramitesAttributeDao.CONTENT_URI,
				null, null);
		return mContext.getContentResolver().delete(TramitesDao.CONTENT_URI,
				null, null);
	}

	public Cursor getTramites() {
		final String sortOrder = TramitesDao.TABLE_NAME + "."
				+ TramitesDao.COL_NAME + " ASC ";
		return mContext.getContentResolver().query(TramitesDao.CONTENT_URI,
				null, null, null, sortOrder);
	}

	public Cursor getTramitesAttributes(long tramiteId) {
		final String sortOrder = TramitesAttributeDao.TABLE_NAME + "."
				+ TramitesAttributeDao.COL_CODE + " ASC ";
		final String selection = TramitesAttributeDao.TABLE_NAME + "."
				+ TramitesAttributeDao.COL_TID + " = ?";
		return mContext.getContentResolver().query(
				TramitesAttributeDao.CONTENT_URI, null, selection,
				new String[] { String.valueOf(tramiteId) }, sortOrder);
	}

	public HashMap<String, ArrayList<TramiteAttributeEntity>> getTramitesLists(
			final long tramiteid) {
		final Cursor cursor = getTramitesAttributes(tramiteid);
		final HashMap<String, ArrayList<TramiteAttributeEntity>> map = new HashMap<String, ArrayList<TramiteAttributeEntity>>();
		final ArrayList<TramiteAttributeEntity> objectivesLst = new ArrayList<TramiteAttributeEntity>();
		final ArrayList<TramiteAttributeEntity> filesNeededLst = new ArrayList<TramiteAttributeEntity>();
		final ArrayList<TramiteAttributeEntity> observationsLst = new ArrayList<TramiteAttributeEntity>();
		final ArrayList<TramiteAttributeEntity> placesLst = new ArrayList<TramiteAttributeEntity>();
		map.put("objectives", objectivesLst);
		map.put("filesNeeded", filesNeededLst);
		map.put("observations", observationsLst);
		map.put("place", placesLst);
		while (!cursor.isAfterLast()) {
			final TramiteAttributeEntity tAttribute = new TramiteAttributeEntity();
			tAttribute.setValue(cursor.getString(cursor
					.getColumnIndexOrThrow(TramitesAttributeDao.COL_VALUE)));
			tAttribute.setCode(cursor.getString(cursor
					.getColumnIndexOrThrow(TramitesAttributeDao.COL_CODE)));

			if (PaperworkAttributeCode.Objective.name().equals(
					tAttribute.getCode())) {
				objectivesLst.add(tAttribute);
			} else if (PaperworkAttributeCode.FileNeeded.name().equals(
					tAttribute.getCode())) {
				filesNeededLst.add(tAttribute);
			} else if (PaperworkAttributeCode.Observation.name().equals(
					tAttribute.getCode())) {
				observationsLst.add(tAttribute);
			} else if (PaperworkAttributeCode.Place.name().equals(
					tAttribute.getCode())) {
				placesLst.add(tAttribute);
			}

			cursor.moveToNext();
		}
		return map;
	}

	public void insertTramites(final JSONArray jSonTramites) {

		for (int i = 0; i < jSonTramites.length(); i++) {
			try {
				long tramiteId = ContentUris.parseId(mContext
						.getContentResolver().insert(
								TramitesDao.CONTENT_URI,
								TramitesDao.contentValuesToInsert(jSonTramites
										.getJSONObject(i))));
				final JSONArray attributes = jSonTramites.getJSONObject(i)
						.getJSONArray("attributeList");
				for (int j = 0; j < attributes.length(); j++) {
					mContext.getContentResolver().insert(
							TramitesAttributeDao.CONTENT_URI,
							TramitesAttributeDao.contentValuesToInsert(
									tramiteId, attributes.getJSONObject(j)));
				}
			} catch (JSONException e) {
				Log.e("jSonError", e.getMessage());
			}
		}
	}

}
